~                       MathPad 2.4

MathPad may be used as a calculator simply by typing in the numbers and operators. When the ENTER key is hit, each line is evaluated and the results are inserted into the text. Variables and functions can be defined. Results can be plotted or tabulated.  Command period can be used to stop evaluation.
~
----------------------- Examples:

21*(3+4)/2.4:61.2

2 +     -- expressions are continued if the line ends with an operator
3*4:14.0

#+5:19.0;    -- # may be used to access the previous result

------- variables and functions
-- calculate a trajectory for initial velocity V, angle Theta.

Vx=V*cos(Theta)
Vy=V*sin(Theta) when Theta > 0 and Theta < 180, 0 otherwise
altitude(t)=Vy*t-(g/2)*t^2
impact_time=2*(Vy/g)
apogee=(Vy^2)/(2*g);   range=Vx*impact_time

g=9.8; V=100;  Theta=75

range:510.2;    apogee:476.0

------- plotting

Xmin=0; Xmax=impact_time
plot altitude(X)
label apogee:476.0

-- Special variables used to control plot:
 --  Title Ystrips
 --  X Xmin Xmax Xsteps Xdiv Xlabel Xlogaxis Xshowgrid Xticklabels
 --  Ymin Ymax Ydiv Ylabel Ylogaxis Yshowgrid Yticklabels
 --  Zmin Zmax Zlabel Zlogaxis Zshowbar
-- newaxis  allows multiple plots/page

------- tables

table     N,       N^3
        1.0        1.0
        2.0        8.0
        3.0       27.0
        4.0       64.0
        5.0      125.0
        6.0      216.0
        7.0      343.0
        8.0      512.0
        9.0      729.0
       10.0     1000.0

-- Special variables used to control table:
 --  N Nmin Nmax Nsteps 

------- summation
series(n) = sum(2*k-1,k,1,n)
series(9):81.0

------- recursion
fact(n) = 1 when n ≤ 0, fact(n-1)*n
fact(50):3.0e+64

------- arrays
A=10,20,30
  -- Defines a 3 element array.
A[2]:20.0;     -- Accesses the 2nd element. Index values start at 1.
B=A,40,50,60,A+1   --Multidimensional array.
B:10.0,20.0,30.0,40.0,50.0,60.0,11.0,21.0,31.0
B[1]:10.0,20.0,30.0
B[1][2]:20.0
B[1,2]:20.0;   -- Both forms of indexing are allowed
A[2:3]:20.0,30.0;    -- [lo:hi] selects a sub array.
A[:2]:10.0,20.0;     -- lo and/or hi may be omitted.

Q[i]=i*11  --Arrays can be defined in terms of their index values.
Q[2]:22.0
-- dim[] can be used to set the number of elements
I[i,j] = 1 when i=j, 0   dim[3,3]
I:1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0

--Arrays may be used freely in expressions. Operations are performed on each element.
A+Q:21.0,42.0,63.0
2+A:12.0,22.0,32.0
log(A):1.0,1.3,1.5
A*Q:110.0,440.0,990.0; --Note: this is not matrix multiply
--Q[A]:? A[?];           --Arrays may not be used as index values

--Functions can use index values
skip(zz,by)[i] = zz[i*by] dim[count(zz)/by]
skip(A,3):30.0
skip(Q,2):22.0,44.0,66.0,...
multiply(A,B)[i,j] = sum(A[i,k]*B[k,j],k,1,count(B)) dim
             [count(A),count(B[1])]
multiply(A,I):10.0,20.0,30.0

--Built-in functions for arrays
count(A):3.0;  -- count() returns the number of array elements
 count(B):3.0;  -- elements in 1st dimension
 count(42):0.0; -- scalar
 count(Q):?;    -- infinite array
det(B):580.0;  -- calculates the determinant of a square matrix
--read("pathname") returns an array of values read from the named file.
--write("pathname",array)  writes the elements of the array to the named file.

--------- The table command can print 1D or 2D arrays
table B
       10.0       20.0       30.0
       40.0       50.0       60.0
       11.0       21.0       31.0

--------- The plot command can plot points from 1D or 2D arrays
-- Arrays can be given in 5 forms:
 -- parametric functions fx(X),fy(X)  X steps 0 to 1.0
 -- array of y values    y1,y2,y3...
 -- array of y functions fy1(X),fy2(X),fy3(X),...
 -- array of x,y pairs   x1,y1,x2,y2,x3,y3,...
 -- a pair of arrays     x1,x2,...,y1,y2,...
-- To connect the points, use "plotline" instead of "plot".
-- For fx(X),fy(X), X is stepped from 0 to 1.0 and the resulting points are plotted in the range Xmin to Xmax.  This can be used for parametric equations:

deg=X*360; Xsteps=36      -- run deg from 0 to 360 in 10° steps
x(angle)=5*cos(angle)+10  -- arbitrary scale to fit on existing plot
y(angle)=100*sin(angle)+200
plot x(deg),y(deg)

--------- The image command can display 2D arrays
img[i,j]=(i-10)^2-5*j^2 dim[20,25]
image img
Zmax=95; Zmin=-500

--------- assignments
-- The assignment operator := evaluates the right hand side expression and replaces any previous definition of the left hand variable with the result. Accessing the variable gives its current value.
-- Assignments can only be done to simple global variables.
-- Finite arrays may be assigned. A single numeric value can be assigned to an element of an array that has previously been initialized by assignment.
-- The result of the := operation itself is always unknown. If you wish to access the result you must use the variable name. Multiple assignments can be performed using comma separated lists.

--------- iteration
-- The 'while' operator can be used to evaluate some expression repeatedly.

factorial(n) = i:=1,m:=1,(i:=i+1,m:=m*i) while i<n, m
factorial(50):3.0e+64

--------- include files
-- include "pathname"  will open and read definitions from the specified file.

---------------------------------------------------
-- Questions and comments to: Mark.Widholm@UNH.edu